
//
//  SEGInputTypeView.m
//  Uhomebk
//
//  Created by mac on 2017/10/30.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "SEGInputTypeView.h"
#import "SEGBottomPopController.h"
#import "SEGOrderInputModel.h"
#import "SEGOrderInputHttpManager.h"

@interface SEGInputTypeView()<DatePickerViewDelegate>
{
    SEGEnumsInputTypeView _type;
    SEGBottomPopController *_breakTimeTypeVC;//预约时间段控件
    SEGBottomPopController *_serviceTypeVC;//服务类型控件
    UIView* dateBgView;
}

@property (nonatomic, strong) NSDate *currentSelectDate;

@end

@implementation SEGInputTypeView

-(void)awakeFromNib
{
    [super awakeFromNib];
    
    self.backgroundColor = kColor_W;
    [_tiitleLabel setTextColor:kColor_1];
    [_contentLabel setTextColor:kColor_C];
    _typeButton.backgroundColor = [UIColor clearColor];
    _lineView.backgroundColor = kColor_L2;
    [_listArrowImage setImage:[UIImage imageWithContentsOfFile:SEGPicName(@"btn_more_gray")]];
    [_typeButton setTitle:@"" forState:UIControlStateNormal];
}

-(void)paddDataWith:(NSDictionary *)dict type:(SEGEnumsInputTypeView)type
{
    _type = type;
    _tiitleLabel.text = dict[@"title"];
    _contentLabel.text = dict[@"content"];
    
    if (type == SEGEnumsInputTypeView_service) {
        SEGOrderInputModel *inputModel = _dataSource;
        if (_serviceTypeVC == nil  ) {
            _serviceTypeVC = [[SEGBottomPopController alloc] init];
            SEGOrderInputModel *inputModel = _dataSource;
            [_serviceTypeVC configDataArrayWithDataArray:inputModel.childrenList popVCType:SEGBottomPopControllerType_ORDER_INPUT_SERVICE_LIST];
        }
        // [需求 11028]默认选择第一个类型
        if (inputModel.childrenList.count >= 1) {
            SEGOrderInputChildModel *childModel = inputModel.childrenList[0];
            self.contentLabel.text = childModel.busiName;
            [self.contentLabel setTextColor:kColor_1];
            [self callbackWithValue:@{@"value":childModel.busiTypeId,@"name":childModel.busiName} type:type];
        }
    }
    else if (type == SEGEnumsInputTypeView_breakTime)
    {
        if (_breakTimeTypeVC == nil){
            _breakTimeTypeVC = [[SEGBottomPopController alloc] init];
            //给选择时间段控件传入默认日期值
            _breakTimeTypeVC.tempValue = [CHECKVALUE(dict[@"dateStr"]) copy];
            self.tempValue = [CHECKVALUE(dict[@"dateStr"]) copy];
            [_breakTimeTypeVC configDataArrayWithDataArray:_dataSource popVCType:SEGBottomPopControllerType_BESPEL_TIME_LIST];
        }
    }
    else if (type == SEGEnumsInputTypeView_breakDate){
        //保存给选择时间段控件传入默认日期值
        self.currentSelectDate = [NSDate date];
        [_contentLabel setTextColor:kColor_1];
    }
}

-(void)configBespealData
{
    if (_breakTimeTypeVC == nil  ) {
        _breakTimeTypeVC = [[SEGBottomPopController alloc] init];
        [_breakTimeTypeVC configDataArrayWithDataArray:_dataSource popVCType:SEGBottomPopControllerType_BESPEL_TIME_LIST];
        return;
    }
    [_breakTimeTypeVC configDataArrayWithDataArray:_dataSource popVCType:SEGBottomPopControllerType_BESPEL_TIME_LIST];
}

-(void)refreshUI
{
    [_breakTimeTypeVC refreshUI];
    [_serviceTypeVC refreshUI];
}

- (IBAction)typeButtonAction:(UIButton *)sender {
    
    switch (_type) {
            //1.选择类型
        case SEGEnumsInputTypeView_service:{
            
            //点击事件回调
            [_serviceTypeVC segBottomPopControllerShowWith:SEGBottomPopControllerType_ORDER_INPUT_SERVICE_LIST cellClick:^(NSDictionary *dict) {
                
                self.contentLabel.text = dict[@"busiName"];
                [self.contentLabel setTextColor:kColor_1];
                [self callbackWithValue:@{@"value":dict[@"busiTypeId"],@"name":dict[@"busiName"]} type:_type];
                
            }];
            break;
        }
            //2.选择预约日期
        case SEGEnumsInputTypeView_breakDate:{
            
            CGSize size = [[UIScreen mainScreen] bounds].size;
            dateBgView= [[UIView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
            dateBgView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
            [[[UIApplication sharedApplication] keyWindow] addSubview:dateBgView];
            
            DatePickerView* datePickerView = [[DatePickerView alloc] initWithFrame:CGRectMake(0, LYScreenHeight() - 250, LYScreenWidth(), 250)];
            datePickerView.delegate = self;
            if (!self.currentSelectDate) {
                self.currentSelectDate = [NSDate date];
            }
            [datePickerView.datePicker setDate:self.currentSelectDate animated:YES];
            [[[UIApplication sharedApplication] keyWindow] addSubview:datePickerView];
            
            break;
        }
            //3.选择预约时间段
        case SEGEnumsInputTypeView_breakTime:{
            //点击事件回调
            //将日期传给时间段控件
            _breakTimeTypeVC.tempValue = self.tempValue;
            [_breakTimeTypeVC segBottomPopControllerShowWith:SEGBottomPopControllerType_BESPEL_TIME_LIST cellClick:^(NSDictionary *dict) {
                NSString *value = dict[@"value"];//提交的值
                NSString *besealTimes = dict[@"bespealTimes"];//展示的值
                if (dict == nil) {
                    value = @"";
                }else{
                    self.contentLabel.text = besealTimes;
                    [self.contentLabel setTextColor:kColor_1];
                }
                
                [self callbackWithValue:@{@"value":value,
                                          @"bespeakEnd":dict[@"bespeakEnd"],
                                          } type:_type];
                
            }];
            break;
        }
    }
}



//时间回调
#pragma mark datapicker delegate
- (void)didDatePickerViewCancelClicked:(DatePickerView*)aPickerView
{
    [dateBgView removeFromSuperview];
    [aPickerView removeFromSuperview];
}

/**
 *  日期选择确定
 *
 *  @param aPickerView 选择框实例
 */
- (void)didDatePickerViewDoneClicked:(DatePickerView*)aPickerView
{
    NSDate* selectDate = [aPickerView selectDate];
    
    if ([selectDate isInPast] && ![selectDate isToday]) {
        [SEGAlertView showTmepMessageWithMessage:@"不能选择今天之前的日期"];
        return;
    }
    _breakTimeTypeVC.tempData = selectDate;
    self.currentSelectDate = selectDate;
    NSString* dateString = [selectDate stringNoUTCDateByFormatString:@"yyyy-MM-dd"];
    self.contentLabel.text = dateString;
    [self.contentLabel setTextColor:kColor_1];
    [self callbackWithValue:@{@"value":dateString} type:_type];
    
    [dateBgView removeFromSuperview];
    [aPickerView removeFromSuperview];
}

/**
 选择日期回调
 
 @param dateStr
 */
- (void)onDateTimePickerValue:(NSString*)dateStr
{
    
}


#pragma mark - ================<回调>===============
-(void)callbackWithValue:(NSDictionary *)value type:(SEGEnumsInputTypeView)type
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(selectInputTypeCallBack:type:)]) {
        [self.delegate selectInputTypeCallBack:value type:type];
    }
}


@end

